package cn.pconline.autoclub.web;

import cn.pconline.autoclub.util.StringUtil;
import cn.pconline.autoclub.util.ZipUtil;
import com.danga.MemCached.MemCachedClient;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TryCatchFinally;
import org.apache.commons.lang.StringUtils;
import org.gelivable.web.EnvUtils;

/**
 * Memcached标签类
 *
 * @author guoqiang.
 * @Time 2014-1-8
 * @Version 1.0
 */
public class MemcachedTag extends BodyTagSupport implements TryCatchFinally {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -3978321366818656050L;
    /**
     * 缓存时间，单位秒，默认600秒
     */
    private long time = 3600;
    /**
     * 缓存Key
     */
    private String key;
    /**
     * 缓存内容
     */
    private String content;
    /**
     * 是否刷新，留空则不刷新
     */
    private String refresh;

    /**
     *
     * @param time
     */
    public void setTime(long time) {
        this.time = time;
    }

    /**
     *
     * @param key
     */
    public void setKey(String key) {
        this.key = key;
    }

    /**
     *
     * @param refresh
     */
    public void setRefresh(String refresh) {
        this.refresh = refresh;
    }

    /**
     *
     * @return
     */
    private String getKey() {
        if (key != null) {
            return key;
        }
        return EnvUtils.getEnv().getRequest().getServletPath() + "?" + StringUtil.md5(EnvUtils.getEnv().getRequest().getQueryString());
    }

    /**
     * starting tag.
     *
     * @return
     * @throws JspException
     */
    @Override
    public int doStartTag() throws JspException {
        if (StringUtils.isNotBlank(refresh) || time <= 0) {
            return EVAL_BODY_BUFFERED;
        }
        content = (String) EnvUtils.getEnv().getBean("mcc", MemCachedClient.class).get(getKey());
        if (content != null) {
            try {
                // 解压缩
                String unzipStr = ZipUtil.uncompress(content);
                pageContext.getOut().write(unzipStr);
            } catch (IOException ex) {
                throw new JspTagException(ex);
            }

            return SKIP_BODY;
        }
        return EVAL_BODY_BUFFERED;
    }

    /**
     * 缓存之后处理的内容
     *
     * @return
     * @throws JspException
     */
    @Override
    public int doAfterBody() throws JspException {
        try {
            content = bodyContent.getString();
            if (time > 0) {
                // 进行压缩之后再存入MC
                String zipStr = ZipUtil.compress(content);
                EnvUtils.getEnv().getBean("mcc", MemCachedClient.class).set(getKey(), zipStr, new java.util.Date(System.currentTimeMillis() + time * 1000));
            }
            bodyContent.clearBody();
            bodyContent.write(content);
            bodyContent.writeOut(bodyContent.getEnclosingWriter());
        } catch (IOException ex) {
            throw new JspTagException(ex);
        }
        return SKIP_BODY;
    }

    /**
     *
     * @param ex
     * @throws Throwable
     */
    @Override
    public void doCatch(Throwable ex) throws Throwable {
        throw ex;
    }

    /**
     * final dealing.
     */
    @Override
    public void doFinally() {
        key = null;
        content = null;
    }
}
